package com.bj58.mis.apm.client.boot;

import com.bj58.mis.apm.client.boot.impl.CollectorManagerImpl;
import com.bj58.mis.apm.client.core.util.PropsUtil;
import com.bj58.mis.apm.client.core.AppNodeContext;
import com.bj58.mis.apm.client.core.BaseCollectLog;
import com.bj58.mis.apm.collector.Collector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Properties;

/**
 * APM客户端
 * <p>
 *     启动客户端<code>APMClient.run()</code>
 * </p>
 * <p>
 *     手动采集日志<code>APMClient.collect(log)</code>
 * </p>
 * @author: zhangfx
 * @create: 2018-06-06 15:31
 **/
public class APMClient {
    private static Logger logger = LoggerFactory.getLogger(APMClient.class);

    private static volatile boolean running = false;

    public static void run(){
        run("apm.properties");
    }

    public static void run(String configPath){
        if(!running){
            synchronized (APMClient.class){
                if(!running){
                    running = true;
                    logger.info("\n\nAPM Client start running!!!\n");
                    Properties props = new Properties();
                    PropsUtil.loadProperties(props, "apm-default.properties");
                    PropsUtil.loadProperties(props, configPath);
                    PropsUtil.printApmProperties(props, "all apm properties");

                    AppNodeContext.init(props);

                    collectorManager = new CollectorManagerImpl();
                }
            }
        }
    }


    private static CollectorManager collectorManager;

    public static CollectorManager getCollectorManager(){
        return collectorManager;
    }

    /**
     * 采集日志API
     * @param log
     */
    public static <Log extends BaseCollectLog> void collect(Log log){
        Collector collector = collectorManager.getCollector(log.getCollectType());
        collector.collect(log);
    }
    public static void main(String[] args){
        APMClient.run("apm.properties");
    }

}
